package com.wyz.demo04;

import com.wyz.demo03.Rent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyInvocationHandler implements InvocationHandler {
    //1.被代理的接口
    private Object object;

    public void setObject(Object object) {
        this.object = object;
    }

    //2.生成得到代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),
                object.getClass().getInterfaces(),this);
    }

    @Override  //处理代理实列，并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质就是使用反射机制实现
        log(method.getName());
        Object result = method.invoke(object, args);
        return result;
    }

    public void log(String mes){
        System.out.println("执行了"+mes+"方法！");
    }
}
